package org.sample.controller;

import org.sample.model.APIResult;
import org.sample.commons.bean.BeanUtils;
import org.sample.config.SessionKey;
import org.sample.dao.ArticleDao;
import org.sample.entity.Article;
import org.sample.entity.Channel;
import org.sample.entity.Group;
import org.sample.entity.User;
import org.sample.entity.enums.CheckState;
import org.sample.service.ArticleService;
import org.sample.service.ChannelService;
import org.sample.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("manage/article")
public class ArticleController {

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ChannelService channelService;

    @Autowired
    private GroupService groupService;

    @RequestMapping(path = {"edit", "add"}, method = RequestMethod.GET)
    public String edit(Article entity, Model model) {
        Integer id = entity.getId();
        if (id != null) {
            entity = articleDao.getOne(id);
        }
        initPageParam(model);
        model.addAttribute("entity", entity);
        return "article/edit";
    }

    @RequestMapping(path = "update", method = RequestMethod.POST)
    public String update(Article entity, HttpSession session) {
        if (entity.getId() == null) {
            User user = (User) session.getAttribute(SessionKey.User);
            entity.setUserId(user.getId());
        } else {
            entity.setUserId(null);
        }
        articleService.saveNotNull(entity);
        return "redirect:edit?id=" + entity.getId();
    }

    @RequestMapping(path = "remove", method = RequestMethod.POST)
    @ResponseBody
    public APIResult remove(Integer id) {
        articleService.logicalDelete(id);
        return new APIResult();
    }

    @RequestMapping("list")
    public String list(@RequestParam(required = false, defaultValue = "1") int pageNo,
                       @RequestParam(required = false, defaultValue = "10") int pageSize,
                       Article entity, Model model) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        BeanUtils.cleanEmpty(entity);
        Page<Article> page = articleDao.findAll(Example.of(entity), pageable);
        model.addAttribute("page", page);

        initPageParam(model);

        model.addAttribute("entity", entity);
        return "article/list";
    }

    private void initPageParam(Model model) {
        List<Channel> channels = channelService.getIdNameList();
        model.addAttribute("channels", channels);

        List<Group> groups = groupService.getIdNameList();
        model.addAttribute("groups", groups);

        CheckState[] checkStates = CheckState.values();
        model.addAttribute("checkStates", checkStates);
    }

}